package cn.edu.gzist.chapter10.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
@Service
public class TaskService {
//    @Async
    public void sendSms() {
        System.out.println("current thread: " + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("--sendSms--end");
    }
    @Async
    public Future<Long> interval() {
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        return CompletableFuture.completedFuture(end - start);
    }

    @Async
    public Future<Boolean> doTask11()  {
        long start = System.currentTimeMillis();
        System.out.println("doTask11 current thread: " + Thread.currentThread().getName());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        System.out.println("任务1耗时:" + (end - start) + "毫秒");
        return CompletableFuture.completedFuture(true);
    }
    @Async
    public Future<Boolean> doTask22()  {
        long start = System.currentTimeMillis();
        System.out.println("doTask22 current thread: " + Thread.currentThread().getName());
        try {
            Thread.sleep(700);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        System.out.println("任务2耗时:" + (end - start) + "毫秒");
        return  CompletableFuture.completedFuture(true);
    }
    @Async
    public Future<Boolean> doTask33() {
        long start = System.currentTimeMillis();
        System.out.println("doTask33 current thread: " + Thread.currentThread().getName());
        try {
            Thread.sleep(600);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        System.out.println("任务3耗时:" + (end - start) + "毫秒");
        return  CompletableFuture.completedFuture(false);
    }

}
